<?php

namespace app\common\library;

use think\Queue;

/**
 * Notes:消息队列
 * DateTime 2021/7/2 9:58
 * @Author wind
 * Class Queues
 * @package queue
 */
class Queues
{
    /**
     * 别名缩写
     * @var string[]
     */
    const ALIAS = [
        'hello'   => 'app\common\job\Hello',
    ];

    /**
     * 检查别名
     * @param $job
     * @return mixed|string
     * @author wind
     * @date 2022-01-24 14:05
     */
    protected static function checkAlias($job) {
        if (in_array($job, array_keys(self::ALIAS))){
            $job = self::ALIAS[$job];
        }
        return $job;
    }

    /**
     * 立即执行
     * @param $job
     * @param $data
     * @param $queue
     * @return mixed
     * @author wind
     * @date 2022-01-24 14:07
     */
    public static function now($job, $data = [], $queue = null)
    {
        $job = self::checkAlias($job);
        return Queue::push($job, $data, $queue);
    }

    /**
     * 指定时间执行
     * @param $timeStamp
     * @param $job
     * @param $data
     * @param $queue
     * @return false
     * @author wind
     * @date 2022-01-24 14:07
     */
    public static function time($timeStamp, $job, $data = [], $queue = null)
    {
        $job = self::checkAlias($job);
        if ($timeStamp < time()){
            return false;
        }
        return Queue::later($timeStamp- time(), $job, $data, $queue);
    }

    /**
     * 延时几秒执行
     * @param $delay
     * @param $job
     * @param $data
     * @param $queue
     * @return mixed
     * @author wind
     * @date 2022-01-24 14:07
     */
    public static function delay($delay,  $job, $data = [], $queue = null)
    {
        $job = self::checkAlias($job);
        return Queue::later($delay, $job, $data, $queue);
    }
}